<template>
  <j-modal title="结案评估信息填写" :width="1200" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <p class="pt">基本信息（必填）</p>
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="人员姓名" prop="realName">
            <a-input v-model="form.realName" placeholder="请输入" :disabled="isDisabled" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="人员类型" prop="personType">
            <a-select v-model="form.personType" placeholder="请选择" style="width:100%;" :disabled="isDisabled">
              <a-select-option value="0">全部</a-select-option>
              <a-select-option v-for="(item, key) in personTypeArr" :key="key" :value="item.value">{{ item.title }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="身份证号" prop="identityCard">
            <a-input v-model="form.identityCard" placeholder="请输入" :disabled="isDisabled" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <p class="pt pl">管控情况</p>
      <div>
        <!-- <TotalInfoItem ref="infoItemId" /> -->
        <control-service :jsonParam="jsonParam" ref="infoItemId" />
      </div>
      <p class="pt pl" style="margin-top: 24px;">结案信息</p>
      <a-row>
        <a-col :span="8">
          <!-- {{ form.caseType }} -->
          <a-form-model-item label="结案类型" prop="caseType">
            <a-select v-model="form.caseType" placeholder="请选择" allowClear @change="onChange">
              <a-select-option v-for="(item, key) in caseTypeArr" :key="key" :value="item.value">{{ item.title }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row class="item2" v-if="form.caseType == 1 && rqUrl != ''">
        <p>社区戒毒社区康复人员期满鉴定书.pdf</p>
        <div class="box">
          <!-- <a-button @click="onView(record)" type="link" style="padding: 0">预览</a-button>
          <a-divider type="vertical" /> -->
          <a-button @click="onDownLoad()" type="link" style="padding: 0">下载</a-button>
        </div>
        <a-tooltip placement="rightBottom" :overlayStyle="{ minWidth: `300px`, maxWidth: `400px` }">
          <template slot="title">
            <div>请将该文件下载签字盖章后上传</div>
          </template>
          <a-icon type="question-circle" />
        </a-tooltip>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="证明材料" prop="prove">
            <j-upload v-model="form.prove" :number="1"></j-upload>
            <p>支持扩展名：.jpg .png .pdf</p>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="10" class="item1">
          <a-form-model-item label="社工总结及反思" prop="reflect">
            <a-textarea v-model="form.reflect" placeholder="请输入" :auto-size="{ minRows: 3, maxRows: 5 }" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-modal>
</template>
<script>
// import TotalInfoItem from '@/views/sjsk/modules/TotalInfoItem'
import controlService from '@/views/sjsk/flow/examine/modules/controlService'
import * as caseEstimateApi from '@/api/sjsk/caseEstimate.js'
import { getDictItemsFromCache } from '@/api/api'
import {
  assessStatisticalToUrine, assessStatisticalToInterview,
  assessStatisticalToReport, assessStatisticalToOther, personSignCount
} from '@/api/sjsk/estimate/list/index'
import * as clinicmanageServiceApi from '@/api/sjsk/clinicmanageService.js'
export default {
  name: 'AddModal',
  components: {
    controlService
    // TotalInfoItem
  },
  data() {
    return {
      visible: false,
      form: {},
      statusValue: undefined,
      isOther: false,
      isDisabled: true,
      personTypeArr: getDictItemsFromCache('personType'),
      caseTypeArr: getDictItemsFromCache('closing_type'),
      rules: {
        realName: [{ required: true, message: '请选择' }],
        personType: [{ required: true, message: '请选择' }],
        identityCard: [{ required: true, message: '请输入' }],
        caseType: [{ required: true, message: '请选择' }],
        prove: [{ required: true, message: '请选择' }],
        reflect: [{ required: true, message: '请输入' }],
      },
      loading: false,
      personId: "",
      jsonParam: {},

      Urinedata: {},
      Interviewdata: {},
      Reportdata: {},
      Otherdata: {},
      Urinedata1: {},
      SignCountdata: {},
      querydata: {},
      sopId: '',
      rqUrl: ""
    }
  },
  created() {
  },
  methods: {
    async show(record, type) {
      this.visible = true
      this.form = {}
      if (record.ceId != null) this.form.id = record.ceId
      if (record.sopId != null) {
        this.form.sopId = record.sopId
        this.personId = record.sopId
      }
      if (record.spId != null) {
        this.form.spId = record.spId
        this.personId = record.spId
      }
      this.form.realName = record.realName
      this.form.personType = record.personType.toString()
      this.form.identityCard = record.identityCard

      // console.log(this.form);
      if (type == 2) { //重新提交,拿之前的信息回填
        this.getDetail(record.ceId)
      }
      this.jsonParam = {
        assessStartTime: record.evalStartTime,
        assessEndTime: record.evalEndTime,
        // sjskPersonId: record.spId,
        sjskPersonId: '1',
        identityCard: record.identityCard,
        caseNo: record.caseNo,
        userId: record.spId,
        signInEndTime: record.evalEndTime,
        signInStartTime: record.evalStartTime,
      }
      this.sopId = record.sopId
      await this.refreshTable()
      this.sopId = ''
      
      //统计信息
      // setTimeout(() => {
      //   this.$refs.infoItemId.setData(record.evalStartTime, record.evalEndTime, this.personId)
      // }, 500)
    },

    // qiandao(record){
    //   clinicmanageServiceApi.getSignInList({pageNo: 1,pageSize: 10,identityCard:record.identityCard}).then((res) => {
    //       this.querydata.userId = res.result.records[0].userId
    //       this.querydata.caseNo = res.result.records[0].caseNo
    //       this.querydata.signInEndTime = record.evalEndTime
    //       this.querydata.signInStartTime = record.evalStartTime
    //   })
    // },
    getDetail(ceId) {
      caseEstimateApi.getCePersonById({ ceId: ceId }).then(res => {
        if (res.success) {
          // this.form.caseType = res.caseType.toString()
          // this.form.prove = res.prove
          // this.form.reflect = res.reflect
          this.form = res.result
        }
      })
    },
    onChange(val) {
      console.log(val)
      if (val == 1) {
        caseEstimateApi.sjskryqmjds(this.personId).then(res => {
          if (res.success) {
            // this.form = res.result
            if (res.success) {
              this.rqUrl = res.result
            } else {
              this.$message.warning("人员期满鉴定书没正常加载。")
            }

          }
        })
      }
    },
    onStatusChange(val) {
      console.log(val.target.value)
      if (val.target.value == 4) {
        this.isOther = true
      } else {
        this.isOther = false
      }
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let str1 = this.$refs.infoItemId.getData1()
          let str2 = this.$refs.infoItemId.getData2()
          let str3 = this.$refs.infoItemId.getData3()
          let str4 = this.$refs.infoItemId.getData4()
          let str5 = this.$refs.infoItemId.getData5()
          let str6 = this.$refs.infoItemId.getData6()
          this.form.urineTest = str1
          this.form.hairTest = str2
          this.form.regularInterviews = str3
          this.form.periodicReport = str4
          this.form.signIn = str5
          this.form.otherIntervent = str6
          this.form.isSubmit = 1 //是否提交到审批
          this.loading = true
          console.log(this.form);
          caseEstimateApi.addClosureEvalByCid(this.form).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
          
        }
      })
    },
    // onView(record) {
    //   window.open('http://172.16.26.68:9300/demo/reportJsp/showReport.jsp?rpx=35_sjskryqmjds.rpx&sjskId=' + record.spId)
    // },
    onDownLoad() {
      window.open(this.rqUrl, "_blank")
    },
    async refreshTable() {
      this.loading = true
      // 尿液检测
      // let params = Object.assign({}, this.querydata, { type: 1 })
      await assessStatisticalToUrine({ ...this.jsonParam, type: 1 }).then((res) => {
        if (res.success) {
          this.Urinedata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 毛发检测
      await assessStatisticalToUrine({ ...this.jsonParam, type: 3 }).then((res) => {
        if (res.success) {
          this.Urinedata1 = res.result ? res.result : {}
          // this.Controldata.Urinedata1 = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 定期访谈
      await assessStatisticalToInterview(this.jsonParam).then((res) => {
        if (res.success) {
          this.Interviewdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 定期报告
      await assessStatisticalToReport(this.jsonParam).then((res) => {
        if (res.success) {
          this.Reportdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 其他介入情况
      await assessStatisticalToOther(this.jsonParam).then((res) => {
        if (res.success) {
          this.Otherdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 签到
      if (!this.sopId) {
        await personSignCount(this.jsonParam).then((res) => {
          if (res.success) {
            this.SignCountdata = res.result ? res.result : {}
            this.loading = false
          } else {
            this.$message.warn(res.message)
            this.loading = false
          }
        })
          .catch((e) => {
            this.loading = false
            this.$message.warn('请求失败！')
          })
      }

      this.jsonParam = {
        Urinedata: this.Urinedata,
        Interviewdata: this.Interviewdata,
        Reportdata: this.Reportdata,
        Otherdata: this.Otherdata,
        Urinedata1: this.Urinedata1,
        SignCountdata: this.SignCountdata,
        sopId: this.sopId
      }
    },
  },
}
</script>
<style lang="less" scoped>
.pt {
  font-weight: bold;
}

.pl {
  padding-top: 20px;
  border-top: 1px dotted #ccc;
}

.ant-row {
  margin-left: 10px;
}

.item1 {
  width: 100%;
  position: relative;
  left: -2%;

  /deep/.ant-col-6 {
    width: 10.5%;
    // text-align: left;
  }

  /deep/.ant-col-16 {
    width: 70%;
    text-align: left;
  }
}

.item2 {
  margin-left: 115px;
  margin-bottom: 20px;

  p {
    display: inline-block;
  }

  .box {
    display: inline-block;
    margin-left: 90px;
    margin-right: 20px;
  }
}
</style>
